﻿using System;
using System.Collections.Generic;
using System.Text;
using PositionSource.WebCam;
using CoordinateCalculator;
using PositionSource;
using RoverStudio.Common;
using RoverStudio.Common.PositionSource;
using RoverStudio.Math;

namespace PositionSourceConfiguration
{
    public class WebcamLEDPositionSourceConfiguration
    {
        private IVideoSourceCommon videoSource;

        public IVideoSourceCommon VideoSource
        {
            get { return videoSource; }
            //set { videoSource = value; }
        }

        private WebcamLEDPositionSource positionSource;

        internal World3DFromPicture2D transformer;

        public event PositionUpdatedHandler RoverPositionUpdated;

        public event NewFrameDelegate NewProcessedFrame
        {
            add
            {
                positionSource.NewFrame += value;
            }
            remove
            {
                positionSource.NewFrame -= value;
            }
        }

        public event NewFrameDelegate NewFrame
        {
            add
            {
                videoSource.NewFrame += value;
            }
            remove
            {
                videoSource.NewFrame -= value;
            }
        }

        public WebcamLEDPositionSourceConfiguration(IVideoSourceCommon videoSource)
        {
            this.videoSource = videoSource;

            positionSource = new WebcamLEDPositionSource(videoSource);

            transformer = new World3DFromPicture2D();

            //positionSource.LedPositionExtractor = ...
            positionSource.TransformationCalculator = transformer;

            positionSource.PositionUpdated += new PositionUpdatedHandler(positionSource_PositionUpdated);

        }

        public void SetReferenceTransformation()
        {
            positionSource.SetReferenceTransformation();
        }


        private void positionSource_PositionUpdated(Vector3D position, Rotation orientation, DateTime timeStamp)
        {
            if (RoverPositionUpdated != null)
                RoverPositionUpdated(position, orientation, timeStamp);
        }
    }
}
